package jp.ne.ayamesoft.ayamevoicedialer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.widget.Toast;

public class AyameVoiceDialerSend extends Activity {
	final int IC_VOICE_RECOGNITION_REQUEST = 0x2001;
	final int INT_MEMBER_MAX = 20;

	Resources resources;
	String dbname_MemberList;
	SQLHelper_MemberList helper_MemberList;
	SQLiteDatabase member_db;
    ArrayList<CMemberData> aryMemberList = new ArrayList<CMemberData>();
	int act_cnt;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		AyameLOG.D("Send onCreate");
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		getMember_From_DB();
		startRecognize();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private int getMember_From_DB(){
		
		AyameLOG.D("searchDB");
        resources = getResources();
    	dbname_MemberList = resources.getString(R.string.dbname_memberlist);
    	helper_MemberList = new SQLHelper_MemberList(this, dbname_MemberList, null);
		member_db = helper_MemberList.getWritableDatabase();
		CMemberData work_memberdata = null;
		
		aryMemberList.clear();
		Cursor cur = getCursorOfMemberList(helper_MemberList);
		act_cnt = 0; 
		boolean isEof = cur.moveToFirst();
		while (isEof) {
			int id = cur.getInt(0);
			String nickname = cur.getString(1);
			String name = cur.getString(2);
			String number = cur.getString(3);
			int atrb = cur.getInt(4);
			isEof = cur.moveToNext();
			
			if((atrb & 0x01) == 0){
				continue;
			}
			
			work_memberdata = new CMemberData();
			work_memberdata.id = id;
			work_memberdata.nickname = nickname;
			work_memberdata.name = name;
			work_memberdata.number = number;
			work_memberdata.atrb = atrb;
			aryMemberList.add(work_memberdata);
		}
		cur.close();
		
		AyameLOG.D("getMember_From_DB,size:" + String.valueOf(aryMemberList.size()));

		return aryMemberList.size();
	}

	private Cursor getCursorOfMemberList(SQLHelper_MemberList hdb) {
		return hdb.getReadableDatabase().query(hdb.STR_TABLE_NAME, new String[] { "id", "nickname", "name", "number", "atrb" }, null, null, null, null, "id");
	}

	private void startSend(String number){
		Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + number));
	    startActivity(intent);
	    finish();
	}
	
	private void startRecognize(){
		
		try{
			Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
					RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT, resources.getString(R.string.speak_nickname));
			intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 20);
			startActivityForResult(intent, IC_VOICE_RECOGNITION_REQUEST);
		}catch(ActivityNotFoundException e){
			Toast.makeText(this, resources.getString(R.string.not_support_voice_reg),
					Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

    	int match_cnt;
    	if (requestCode == IC_VOICE_RECOGNITION_REQUEST && resultCode == RESULT_OK) {
    		
            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            match_cnt = matches.size();
			AyameLOG.D("match_cnt =" + String.valueOf(match_cnt));
            if( match_cnt == 0 )
            {
            }
            else
            {
            	for(int i=0;i<match_cnt;i++)
            	{
            		AyameLOG.D("matches[" + String.valueOf(i) + "] = [" + matches.get(i) + "]");
            		for(int j=0;j<aryMemberList.size();j++){
            			if(matches.get(i).equals(aryMemberList.get(j).nickname)){
            				AyameLOG.D("match , nickname:" + aryMemberList.get(j).nickname + " name:" + aryMemberList.get(j).name + " number:" + aryMemberList.get(j).number );
            				startSend(aryMemberList.get(j).number);
            				finish();
            				return;
            			}
            		}
            		
            	}
				AyameLOG.D(" no match!!");
            }
    	}
	    finish();
	    
	}
	
}
